package com.nhjf.mobile.controller.admin.user;


import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.model.cn_nojd.Cnnojd;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.Resource;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.OperationLog;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.user.TeacherDto;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.cn_nojd.CnnojdService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.ResourceService;
import com.nhjf.service.system.RoleService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.OperationLogService;
import com.nhjf.service.teachers.TeachersService;
import com.nhjf.mobile.common.util.Constants;
import com.nhjf.mobile.common.util.SysProperties;

import net.sf.json.JSONObject;

/**
 * @类功能说明：用户管理
 * @部门：技术部
 * @作者：龚一凡
 * @版本：V1.0
 */
@Controller
@RequestMapping(value = "/userAdmin")
public class UserAdminController{

	@Autowired
	private OperationLogService operationLogService;

	@Autowired
	private UserService userService;

	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private TeachersService teacherService;
	@Autowired
	private CnnojdService cnnojdService;

	/**
	 * 
	 * @方法功能说明：用户列表
	 * @修改者名字：龚一凡
	 * @修改内容：
	 * @参数：
	 * @return:
	 * @throws
	 */
	@RequestMapping(value = "/user_list")
	public String queryUserList(HttpServletRequest request, Model model,@RequestParam(value="page", defaultValue="1")Integer page){


		return "/system/query_user.html";
	}
	/**
	 * 修改当前登陆人密码
	 * @param curPwd
	 * @param newPwd
	 * @param conNewPwd
	 * @param userQo
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/modifyUserPwd")
	public String updatePwd(String curPwd,String newPwd,String conNewPwd,HttpServletRequest request,HttpServletResponse response,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo1=new UserQo();
				userQo1.setSeachRole(true);
				userQo1.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo1);
				if(user!=null) {
					if(curPwd.equals(user.getUserPassword())) {
						if(newPwd.equals(conNewPwd)) {
							user.setUserPassword(newPwd);
							userService.update(user);
							OperationLog operationLog=new OperationLog();
							operationLog.setOperationType(3);
							operationLog.setOperationDescription("修改当前登陆人密码!");
							operationLog.setUserId(user);
							operationLog.setOperationTime(new Date());
							operationLog.setOperationUserName(user.getUserName());
							operationLogService.save(operationLog);
							resMap.put("code", "200");
							resMap.put("message", "修改成功!");
							return JSONObject.fromObject(resMap).toString();
						}else {
							resMap.put("code", "500");
							resMap.put("message", "两次密码不一致!");
							return JSONObject.fromObject(resMap).toString();
						}

					}else {
						resMap.put("code", "500");
						resMap.put("message", "请输入正确的密码!");
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					resMap.put("code", "406");
					resMap.put("message", "请先登录!");
					return net.sf.json.JSONObject.fromObject(resMap).toString();
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "操作异常!");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常!");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	/**
	 * 查看等钱登陆人大概信息
	 * @param token
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/viewProbably")
	public String viewProbably(@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo1=new UserQo();
				userQo1.setSeachRole(true);
				userQo1.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo1);
				if(user!=null) {
					if(user.getIsRole()==2 || user.getIsRole()==4) {
						resMap.put("code", "500");
						resMap.put("message", "最高管理员和学生不能登陆!");
						return JSONObject.fromObject(resMap).toString();
					}
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setSeachOrganization(true);
					teachersQo.setSeachUser(true);
					teachersQo.setUserId(user.getId());
					Teacher teacher= teacherService.queryUnique(teachersQo);
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(teacher!=null) {
						TeacherDto teacherDto=new TeacherDto();
						if(StringUtils.isNotBlank(teacher.getTraininginstitution().getLogo())) {
							teacherDto.setAvatar(imagePath+"/logo/"+teacher.getTraininginstitution().getLogo());
						}
						if(user.getCity()!=null) {
							Cnnojd province= cnnojdService.get(user.getCity());
							teacherDto.setCity(province.getName());
						}
						teacherDto.setIdentity(user.getIsRole().longValue());//1代表老师3代表机构管理员
						teacherDto.setNickName(teacher.getTraininginstitution().getName());
						resMap.put("code", "200");
						resMap.put("message", teacherDto);
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					resMap.put("code", "406");
					resMap.put("message", "请先登录!");
					return net.sf.json.JSONObject.fromObject(resMap).toString();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常!");
			return JSONObject.fromObject(resMap).toString();
		}
		resMap.put("code", "500");
		resMap.put("message", "系统异常!");
		return JSONObject.fromObject(resMap).toString();
	}
}
